package com.gomore.opple.module.award.lotteried;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventReflashLotteriedFragment;
import com.gomore.opple.module.BaseFragmentV4;
import com.gomore.opple.module.award.adapter.AwardAdapter;
import com.gomore.opple.service.UseJDEClicklistener;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;


/**
 * Created by robot
 */
public class LotteriedFragment extends BaseFragmentV4 implements LotteriedContract.View,
        PullBaseView.OnRefreshListener {

    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;
    private LotteriedContract.Presenter mPresenter;
    private AwardAdapter awardAdapter;


    public static LotteriedFragment getInstance() {
        return new LotteriedFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_lotteried;
    }

    @Override
    protected void initalizeData() {
        EventBus.getDefault().register(this);
        mPresenter.queryAward(false);
    }

    @Override
    protected void initalizeViews() {
        awardAdapter = new AwardAdapter(getActivity(), getActivity(), R.layout.lottery_item, mPresenter.getData(), GlobalConstant.AwardState.ACCOMPLISHED);
        recyclerView.setLayoutManager(new LinearLayoutManager(LotteriedFragment.this.getActivity()));
        recyclerView.setAdapter(awardAdapter);
        recyclerView.setOnRefreshListener(this);
    }


    @Override
    public void setPresenter(LotteriedContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showContentView() {
        awardAdapter.notifyDataSetChanged();
        if (mPresenter.getData().size() == 0) {
            empty_view.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            empty_view.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    public void onEventMainThread(EventReflashLotteriedFragment eventReflashLotteriedFragment) {
        if (eventReflashLotteriedFragment != null && eventReflashLotteriedFragment.isReflash()) {
            mPresenter.queryAward(false);
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        mPresenter.queryAward(false);
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        mPresenter.queryAward(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
